import es6Promise from "es6-promise";
import fetch from "isomorphic-fetch";
import {router} from '../router/index';
es6Promise.polyfill();
module.exports = function (the, url, data, successFun, failFun, errFun) {
	var options = {
        credentials: 'include',
		method: 'POST',
		headers: {'Content-Type': 'application/json; charset=utf-8', 'node': 'open'},
		body: JSON.stringify(data)
	};
	fetch(url, options).then(response => {
		if (response.status >= 200 && response.status < 300) {
			return response.json();
		} else {
			var error = new Error(response.statusText);
			error.response = response;
			throw error;
		}
	}).then(data => {
		if (data.code != 0) {
			if(data.code=="10102"){
                router.push({
                    name: 'login'
                });
			}
			failFun && failFun(data);
		} else {
            successFun && successFun(data);
		}
	}).catch(e => {
		errFun && errFun(e);
	});
};